using System;
using System.Windows.Forms;
using LibrErian.Data;
using LibrErian.Model;

namespace LibrErian
{
	public partial class frmEditBook : Form
	{
		private Book currentBook = new Book();

		public frmEditBook()
		{
			InitializeComponent();
		}

		public void EditBook(Book book)
		{
			currentBook = book;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			NHibernateManager.SaveOrUpdate(currentBook);
			DialogResult = DialogResult.OK;
		}

		private void frmEditBook_Load(object sender, EventArgs e)
		{
			LoadAuthors();

			InitializeBindings();
		}

		private void InitializeBindings()
		{
			tbTitle.DataBindings.Add("Text", currentBook, "Title");
			tbSubtitle.DataBindings.Add("Text", currentBook, "Subtitle");
			tbISBN.DataBindings.Add("Text", currentBook, "Isbn");
			tbPages.DataBindings.Add("Text", currentBook, "Pages");
			tbSummary.DataBindings.Add("Text", currentBook, "Summary");
			dtpPublicationDate.DataBindings.Add("Value", currentBook, "PublicationDate");

			cmbAuthor.DataBindings.Add("SelectedItem", currentBook, "Author");
		}

		private void LoadAuthors()
		{
			cmbAuthor.DataSource = NHibernateManager.GetAuthors();
			cmbAuthor.DisplayMember = "Name";
		}
	}
}